package com.qk.management.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.common.constant.BusinessStatusConstants;
import com.qk.domain.business.BusinessDO;
import com.qk.dto.business.BusinessDTO;
import com.qk.dto.business.BusinessListDTO;
import com.qk.dto.business.BusinessPoolDTO;
import com.qk.dto.business.UpdateBusinessInfoDTO;
import com.qk.entity.Business;
import com.qk.management.service.BusinessService;
import com.qk.vo.business.BusinessVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;


@RestController
@RequestMapping("/businesses")
public class BusinessController {
    @Autowired
    private BusinessService businessService;
    @GetMapping("/pool")
    public Result pool(BusinessPoolDTO businessPoolDTO) {
        PageResult<Business> list = businessService.selectPool(businessPoolDTO);
        return Result.success(list);
    }
    @PostMapping("/toCustomer/{id}")
    public Result toCustomer(@PathVariable Integer id) {
        businessService.toCustomer(id);
        return Result.success();
    }
    @PutMapping("/back/{id}")
    public Result back(@PathVariable Integer id) {
        businessService.updateById(Business.builder()
                .id(id)
                .status(BusinessStatusConstants.STATUS_RECOVERY)
                .updateTime(LocalDateTime.now())
                .build());
        return Result.success();
    }
    @PutMapping
    public Result updateById(@RequestBody UpdateBusinessInfoDTO dto){
        businessService.updateBusinessInfoById(dto);
        return Result.success();
    }
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        BusinessVO businessVO = businessService.selectById(id);
        return Result.success(businessVO);
    }
    @PutMapping("/assign/{businessId}/{userId}")
    public Result update(@PathVariable Integer businessId, @PathVariable Integer userId){
        businessService.updateById(Business.builder()
                .id(businessId)
                .userId(userId)
                .updateTime(LocalDateTime.now())
                .status(BusinessStatusConstants.STATUS_WAIT_FOLLOW)
                .build());
        return Result.success();
    }

    @PostMapping
    public Result add(@RequestBody BusinessDTO businessDTO) {
        businessService.add(businessDTO);
        return Result.success();
    }
    @GetMapping
    public Result list(BusinessListDTO businessListDTO) {
        PageResult<BusinessDO> list = businessService.selectByPage(businessListDTO);
        return Result.success(list);
    }

}
